<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var longestPalindrome = function (words) {
      let hashMap = {}, res = 0;
      for (let i = 0; i < words.length; i++) {
        const reverseWord = words[i][1] + words[i][0];
        if (hashMap[reverseWord]) {
          res += 4;
          hashMap[reverseWord] -= 1;
        } else {
          hashMap[words[i]] = (hashMap[words[i]] || 0) + 1;
        }
      }
      for (let word in hashMap) {
        if (hashMap[word] && word[0] === word[1]) return res + 2;
      }
      return res;
    };
    console.log(longestPalindrome(["lc", "cl", "gg"]))
  </script>
</body>

</html>